import CommonHeader from '@/components/common-header/index'
import SelectInstanceById from '@/apis/SelectInstanceById'
import QueryAprovals from '@/apis/QueryAprovals'
import CompleteTask from '@/apis/CompleteTask'
import { goTo } from '@/utils/goTo'

export default {
  name: 'DisplayDetail',
  components: {
    CommonHeader,
  },
  data() {
    return {
      processId: '',
      approvalType: null,
      displayDetailInfo: {},
      selectStateList: [
        '待审批',
        '审批中',
        '已通过',
        '已驳回',
        '已撤销',
        '待销申请',
      ],
      scheduleList: [],
      displaySpecialInfo: [],
      message: '',
    }
  },
  onLoad(options) {
    // console.log(options.data)
    this.processId = JSON.parse(options.data).processId
    this.approvalType = JSON.parse(options.data).approvalType
    // 查询详细内容
    SelectInstanceById(
      {
        processId: this.processId, //业务id
      },
      this.$store.state.authorization
    ).then((res) => {
      // console.log('详细内容', res)
      this.displayDetailInfo = res.data.data
      const rareData = res.data.data.procData
      // console.log(res.data.data.processName, rareData)
      rareData
        ? this.switchInfo(res.data.data.processName, JSON.parse(rareData))
        : null
    })
    // 查询流程进度
    QueryAprovals(
      {
        processId: this.processId, //业务id
      },
      this.$store.state.authorization
    ).then((res) => {
      let scheduleList = res.data.data
      this.scheduleList = scheduleList
      // console.log(this.scheduleList)
    })
  },
  methods: {
    applicationCanceled() {
      uni.showLoading({
        title: '加载中',
      })
      CompleteTask(
        {
          jobNumber: this.$store.state.userInfo.jobNumber,
          processId: this.processId, //业务id
          order: '4',
          handleOpinion: '<用户没有填写信息>',
        },
        this.$store.state.authorization
      ).then((res) => {
        // console.log(res)
        if (res.data.flag) {
          uni.hideLoading()
          this.$toast('撤销成功')
          setTimeout(() => {
            goTo('/pages/main/index')
          }, 600)
        }
      })
    },
    approvalSuccess() {
      const obj = {
        jobNumber: this.$store.state.userInfo.jobNumber,
        processId: this.processId, //业务id
        order: '2',
        handleOpinion:
          this.message === '' ? '<用户没有填写信息>' : this.message,
      }
      if (this.displayDetailInfo.processName === '销假、销出差') {
        obj['leaveId'] = JSON.parse(this.displayDetailInfo.procData).leaveId
      }
      uni.showLoading({
        title: '加载中',
      })
      CompleteTask(obj, this.$store.state.authorization).then((res) => {
        // console.log(res)
        if (res.data.flag) {
          uni.hideLoading()
          this.$toast('已通过')
          setTimeout(() => {
            goTo('/pages/main/index')
          }, 600)
        }
      })
    },
    approvalRejected() {
      uni.showLoading({
        title: '加载中',
      })
      CompleteTask(
        {
          jobNumber: this.$store.state.userInfo.jobNumber,
          processId: this.processId, //业务id
          order: '3',
          handleOpinion:
            this.message === '' ? '<用户没有填写信息>' : this.message,
        },
        this.$store.state.authorization
      ).then((res) => {
        // console.log(res)
        if (res.data.flag) {
          uni.hideLoading()
          this.$toast('已驳回')
          setTimeout(() => {
            goTo('/pages/main/index')
          }, 600)
        }
      })
    },
    // 日期格式化
    formDate(date) {
      let year = new Date(date).getFullYear()
      let month =
        new Date(date).getMonth() + 1 < 10
          ? '0' + (new Date(date).getMonth() + 1)
          : new Date(date).getMonth()
      let day =
        new Date(date).getDate() < 10
          ? '0' + new Date(date).getDate()
          : new Date(date).getDate()
      let hours =
        new Date(date).getHours() < 10
          ? '0' + new Date(date).getHours()
          : new Date(date).getHours()
      let minutes =
        new Date(date).getMinutes() < 10
          ? '0' + new Date(date).getMinutes()
          : new Date(date).getMinutes()
      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
    },
    // IOS兼容问题
    formDateIOS(date) {
      return date ? date.split(':')[0] + ':' + date.split(':')[1] : ''
    },
    formDay(date) {
      let year = new Date(date).getFullYear()
      let month =
        new Date(date).getMonth() + 1 < 10
          ? '0' + (new Date(date).getMonth() + 1)
          : new Date(date).getMonth()
      let day =
        new Date(date).getDate() < 10
          ? '0' + new Date(date).getDate()
          : new Date(date).getDate()
      return year + '-' + month + '-' + day
    },
    goTo,
    switchInfo(type, data) {
      switch (type) {
        case '请假':
          this.displaySpecialInfo = [
            {
              label: '请假类型',
              value: data.type,
            },
            {
              label: '开始时间',
              value: this.formDate(data.startTime),
            },
            {
              label: '结束时间',
              value: this.formDate(data.endTime),
            },
            {
              label: '请假时长',
              value: data.duration + '天',
            },
            {
              label: '请假事由',
              value: data.leaveReason,
            },
          ]
          break
        case '调岗':
          this.displaySpecialInfo = [
            {
              label: '入职日期',
              value: this.formDay(data.hireDate),
            },
            {
              label: '生效日期',
              value: this.formDay(data.effectiveDate),
            },
            {
              label: '原部门',
              value: data.oldDepartment,
            },
            {
              label: '原职位',
              value: data.oldJob,
            },
            {
              label: '新部门',
              value: data.newDepartment,
            },
            {
              label: '新职位',
              value: data.newJob,
            },
          ]
          break
        case '调薪':
          this.displaySpecialInfo = [
            {
              label: '基本工资',
              value: data.baseSalary,
            },
            {
              label: '调薪额度',
              value: data.adjustLines,
            },
            {
              label: '调薪原因',
              value: data.adjustReason,
            },
          ]
          break
        case '补卡':
          this.displaySpecialInfo = [
            {
              label: '补卡日期',
              value: this.formDay(data.fillClockTime),
            },
            {
              label: '补卡理由',
              value: data.fillClockReason,
            },
          ]
          break
        case '转正':
          this.displaySpecialInfo = [
            {
              label: '入职日期',
              value: this.formDay(data.hireDate),
            },
            {
              label: '试用期工作总结',
              value: data.jobSummary,
            },
            {
              label: '试用期岗位',
              value: data.trialJob,
            },
            {
              label: '对岗位的理解',
              value: data.trialJobText,
            },
            // {
            //   label: '对公司的意见与建议',
            //   value: data.advise,
            // },
          ]
          break
        case '报销':
          this.displaySpecialInfo = [
            {
              label: '报销类型',
              value: data.reimbursementType,
            },
            {
              label: '实际报销金额',
              value: data.acAmount,
            },
            {
              label: '借款金额',
              value: data.boAmount,
            },
            {
              label: '报销金额',
              value: data.reAmount,
            },
            // 发票编号
          ]
          break
        case '离职':
          this.displaySpecialInfo = [
            {
              label: '交接人员',
              value: data.connectEmployee,
            },
            {
              label: '入职日期',
              value: this.formDay(data.entryDate),
            },
            {
              label: '离职日期',
              value: data.separationDate,
            },
            {
              label: '所属岗位',
              value: data.job,
            },
            {
              label: '离职原因',
              value: data.separationReason,
            },
          ]
          break
        case '出差':
          this.displaySpecialInfo = [
            {
              label: '出差地点',
              value: data.address,
            },
            {
              label: '出行工具',
              value: data.travelTools,
            },
            {
              label: '出差开始时间',
              value: this.formDate(data.startTime),
            },
            {
              label: '出差结束时间',
              value: this.formDate(data.endTime),
            },
            {
              label: '出差时间',
              value: data.days + '天',
            },
            {
              label: '出差原因',
              value: data.travelReason,
            },
          ]
          break
        case '销假、销出差':
          this.displaySpecialInfo = [
            {
              label: '销假时间',
              value: this.formDate(data.destroyTime),
            },
          ]
          break
        case '加班':
          this.displaySpecialInfo = [
            {
              label: '加班开始时间',
              value: this.formDate(data.startTime),
            },
            {
              label: '加班结束时间',
              value: this.formDate(data.endTime),
            },
            {
              label: '加班时间',
              value: data.duration + '天',
            },
            {
              label: '加班原因',
              value: data.overtimeReason,
            },
          ]
          break
      }
    },
  },
}
